package com.hong.springboot.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import org.zeromq.SocketType;
import org.zeromq.ZMQ;

import javax.annotation.PostConstruct;

@Configuration
@Service
public class ZeroMQProducer {


    private ZMQ.Context context;

    private ZMQ.Socket socket;

    private SocketType mode = SocketType.PUSH;

    @Value("${spring.zeromq.publisher.ioNum}")
    private Integer ioNum = 1;
    @Value("${spring.zeromq.subscriber.address}")
    private String address;

    public Integer getIoNum() {
        return ioNum;
    }

    public void setIoNum(Integer ioNum) {
        this.ioNum = ioNum;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @PostConstruct
    public void init(){
        context = ZMQ.context(ioNum);
        socket = context.socket(mode);
        socket.bind(address);
    }

    public boolean send(String msg) {
        return socket.send(msg, ZMQ.NOBLOCK);
    }
}